package com.cyy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.base.exception.CommonError;
import com.cyy.base.exception.LearningException;
import com.cyy.content.mapper.CourseTeacherMapper;
import com.cyy.content.model.po.CourseTeacher;
import com.cyy.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author cyy
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<CourseTeacher> courseTeacherList(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(courseId != null, CourseTeacher::getCourseId, courseId);
        return courseTeacherMapper.selectList(wrapper);
    }

    @Override
    public void editCourseTeacher(CourseTeacher courseTeacher) {
        if (courseTeacher == null) {
            LearningException.cast(CommonError.OBJECT_NULL);
        }
        courseTeacherMapper.updateById(courseTeacher);
    }

    @Override
    public void deleteCourseTeacher(Long courseId, Long teacherId) {
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(courseId != null, CourseTeacher::getCourseId, courseId);
        wrapper.eq(teacherId != null, CourseTeacher::getId, teacherId);
        courseTeacherMapper.delete(wrapper);

    }
}
